// pages/user/editAddress/editAddress.js
import userIndex  from '../../../api/user';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    provinceName:'',
    provinceId:'',
    phone:'',
    name:'',
    isdefault:0,
    id:'',
    districtName:'',
    districtId:'',
    cityName:'',
    cityId:'',
    address:'',

    // province:[],//省
    // city:[],//市
    // district:[],//区
    region:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
    if(options.id){
      this.getAddressInfo()
    }
    // this.getUseraddressArea('province',100000)
  },
  getAddressInfo(){
    userIndex.apiUseraddressInfo({id:this.data.id}).then(res=>{
      this.setData({
        provinceName:res.data.provinceName,
        provinceId:res.data.provinceId,
        phone:res.data.phone,
        name:res.data.name,
        isdefault:res.data.isdefault||0,
        districtName:res.data.districtName,
        districtId:res.data.districtId,
        cityName:res.data.cityName,
        cityId:res.data.cityId,
        address:res.data.address,
      })
    })
  }, 
  getUseraddressArea(name,parentId){
    userIndex.apiUseraddressArea({parentId:parentId}).then(res=>{
      this.setData({
        [name]:res.data
      })
    })
  },
  getInput(e){
    
    var name=e.currentTarget.dataset.name
    var value=e.detail.value
    this.setData({
      [name]:value
    })
  },
  saveBtn(){
    var opt={
      provinceName:this.data.provinceName,
      provinceId:this.data.provinceId,
      phone:this.data.phone,
      name:this.data.name,
      isdefault:this.data.isdefault,
      districtName:this.data.districtName,
      districtId:this.data.districtId,
      cityName:this.data.cityName,
      cityId:this.data.cityId,
      address:this.data.address,
     
    }
    if(this.data.id){
      opt.id=this.data.id
    }
    if(!opt.name){
      wx.showToast({
        title: '请填写收货人姓名',
        icon:'none'
      })
      return false
    }
    if(!opt.phone){
      wx.showToast({
        title: '填写收货人手机号',
        icon:'none'
      })
      return false
    }
    if(!opt.address){
      wx.showToast({
        title: '填写收货人详细地址',
        icon:'none'
      })
      return false
    }
  
    userIndex.apiUseraddressSetSave(opt).then(res=>{
      wx.showToast({
        title: '保存成功',
        icon:'none'
      })
      wx.navigateBack({
        delta:1
      })
    })
  },
  bindRegionChange(e){
   this.setData({
    region:e.detail.value,
    provinceName:e.detail.value[0],
    provinceId:e.detail.code[0],
    cityName:e.detail.value[1],
    cityId:e.detail.code[1],
    districtName:e.detail.value[2],
    districtId:e.detail.code[2],
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})